//
//  CZNewFeatureViewController.swift
//  swiftweibo
//
//  Created by Apple on 15/11/26.
//  Copyright © 2015年 tzw. All rights reserved.
//

import UIKit
// 新特性界面
class CZNewFeatureViewController: UICollectionViewController {
    // 注册流水布局
    private let ReuseIdentifier = "ReuseIdentifier"
    // 显示cell的数量
    private let imageCount = 4
    // 创建流水布局
    private var flowlayout = UICollectionViewFlowLayout()
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 自己来完成流水布局
    init(){
        super.init(collectionViewLayout: flowlayout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // 准备流水布局
        prepareCollectionView()
    }
    private func  prepareCollectionView(){
        // 注册流水布局
        collectionView?.registerClass(CZNewFeatureCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        // 设置布局
        // 设置item的大小 
        flowlayout.itemSize = view.bounds.size
        // 设置滚动的方向 
        flowlayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        // 设置间距
        flowlayout.minimumInteritemSpacing = 0
        flowlayout.minimumLineSpacing = 0
        // 设置弹簧效果
        collectionView?.bounces = false
        //设置分页
        collectionView?.pagingEnabled = true
        
    }
    // MARK: - collection的数据源方法
    // 要显示cell的数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    // 显示cell的内容
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! CZNewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    // 当cell.collectionView不可见的时候调用 
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        // 获取正在显示的cell
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        // 判断当前的显示是否是最后一个
        if visibleIndexPath.item == imageCount - 1{
            // 根据indexPath获取cell
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! CZNewFeatureCell
            // 做动画
            cell.startButtonAnination()
            
        }
    }

}

// 自定义cell

class CZNewFeatureCell: UICollectionViewCell {
    // 修改显示的图片
    private var index: Int = 0{
        didSet {
            // 根据是第几个cell来设置图片
            iconView.image = UIImage(named: "new_feature_\(index+1)")
            startButton.hidden = true
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 准备UI
        prepareUI()
    }
    // 开始动画
    func startButtonAnination(){
        // 显示按钮
        startButton.hidden = false
        // 设置按钮的缩放
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0.1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("动画完成")
        }
    }
   private func prepareUI(){
    contentView.addSubview(iconView)
    contentView.addSubview(startButton)
    // 添加约束
//    iconView.translatesAutoresizingMaskIntoConstraints = false
//    startButton.translatesAutoresizingMaskIntoConstraints = false
    // 水平方向
    iconView.ff_Fill(contentView)
//    contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[biv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : iconView]))
//    contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[biv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["biv" : iconView]))
//    
    // 给按钮添加约束
    startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint.init(x: 0, y: -160))
//    contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//    contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    // MARK: - 点击体验按钮
     func buttonClick(){
        print("aaaa")
        // 切换控制器
        AppDelegate.switchRootViewController(true)
    }
    
    // MARK: - 懒加载
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    // 创建按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        // 设置文字 
        button.setTitle("立即体验", forState: UIControlState.Normal)
        // 监听按钮
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
}